package nbcp.myoql.code.generator.db.mysql

import nbcp.base.comm.config
import nbcp.base.extend.AsBoolean
import nbcp.base.extend.AsString
import nbcp.base.extend.HasValue
import nbcp.base.utils.StringUtil
import nbcp.myoql.db.sql.enums.DbType


class EntityDbItemFieldData {
    // name 小驼峰形式。
    val fieldName: String
        get() {
            var name2 = this.name;
            if (name2[0].isDigit()) {
                name2 = "field_" + name2;
            }

            if (config.myoqlKeepDbName) {
                return StringUtil.splitWordParts(name2).joinToString("_");
            }
            return StringUtil.getSmallCamelCase(name2);
        }

    var sqlType = ""
    var charLength = 0L

    /**
     * 数据库列名
     */
    var name = ""
    var dbType = DbType.OTHER
    var comment = ""
    var isPrimary = false
    var isUnique = false
    var index = "";
    var isVirtual = false

    val simpleComment: String
        get() {
            return Regex("""\s*\(\s*(auto_id)|(auto_number)\s*\)\s*""").replace(comment, "")
        }

    val kotlinType: String
        get() {
            var userType = dbType.getUserDefineColumnType(this.name, this.comment);
            if (userType.HasValue) {
                if (userType.contains("<") && userType.contains(">")) {
                    return userType;
                }
                return userType + "?"
            }
            return this.dbType.toKotlinType(userType)
        }

    val javaType: String
        get() {
            return dbType.getUserDefineColumnType(this.name, this.comment).AsString(this.dbType.javaType.typeName)
        }

    val kotlinDefaultValue: String
        get() {
            var userType = dbType.getUserDefineColumnType(this.name, this.comment);
            if (userType.HasValue) {
                if (userType.contains("<") && userType.contains(">")) {
                    return "setOf()";
                }
                return "null"
            }

            return this.dbType.toKotlinDefaultValue()
        }

    val javaDefaultValue: String
        get() {
            if (this.dbType == DbType.BYTE) {
                return "new byte[0]"
            }
            return this.dbType.toJavaDefaultValue()
        }

    var autoId: Boolean = false
        get() {
            if (autoInc) {
                return false;
            }
            if (comment.contains(Regex("\bauto_id\b", RegexOption.IGNORE_CASE))) {
                return true;
            }


            var idIsAutoId = fieldName == "id" && dbType == DbType.STRING && config.getConfig("app.sql.id-is-autoId")
                .AsBoolean(true);
            if (idIsAutoId) {
                return idIsAutoId;
            }
            return false;
        }

    var autoNumber: Boolean = false
        get() {
            if (autoInc) {
                return false;
            }

            if (comment.contains(Regex("\bauto_number\b", RegexOption.IGNORE_CASE))) {
                return true;
            }

            var idIsAutoNumber =
                fieldName == "id" && dbType == DbType.LONG && config.getConfig("app.sql.id-is-autoNumber").AsBoolean();
            if (idIsAutoNumber) {
                return idIsAutoNumber;
            }
            return false;
        }

    /*下面四个属性表示该表的单键主键 或 唯一键*/
    var autoInc: Boolean = false


    fun getRemark(): String {
        if (autoInc) {
            return "[自增] " + this.simpleComment
        }
        if (autoId) {
            return "[雪花Id字符串] " + this.simpleComment
        }
        if (autoNumber) {
            return "[雪花Id数值] " + this.simpleComment
        }
        return this.simpleComment
    }
}

